﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public delegate void ChangedEventHandler(object sender, EventArgs e);

    public class GestionNotification
    {
        public event ChangedEventHandler Changed;

        protected virtual void OnChanged(EventArgs e)
        {
            if (Changed != null)
                Changed(this, e);
        }

        public void Ajouter(Notification notification)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Notifications.AddObject(notification);
                    db.SaveChanges();
                    OnChanged(EventArgs.Empty);
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public List<Notification> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Notifications.ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Notification Chercher(int notificationId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Notifications.Where(n => n.Id == notificationId).FirstOrDefault();   
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Notification> Lister(Commande commande)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from n in db.Notifications
                            where n.CommandeId == commande.Id
                            select n).ToList();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur Repository : Echec Recuperation toutes les notifications", ex);
            }
        }

        public List<Notification> Lister(string mailUtilisateur)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from n in db.Notifications.Include("Commande.Plat")
                            where n.Commande.Serveur.Email == mailUtilisateur
                            select n).ToList();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur Repository : Echec Recuperation toutes les notifications", ex);
            }
        }

        public List<Notification> Lister(string mailUtilisateur,bool valide)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from n in db.Notifications
                            where n.Commande.Serveur.Email == mailUtilisateur && n.Fait == valide
                            select n).ToList();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur Repository : Echec Recuperation toutes les notifications", ex);
            }
        }

        public List<Notification> ListerParType(string typeNotification)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from n in db.Notifications
                                where n.Type == typeNotification
                                select n).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MarquerCommeFait(int notificationId)
        {
            try
            {            
                Notification notif = Chercher(notificationId);
                notif.Fait = true;

                MettreAJour(notif);
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MarquerCommeFait(Notification notification)
        {
            notification.Fait = true;
            MettreAJour(notification);
        }

        public void MettreAJour(Notification notification)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Notifications.Attach(notification);
                    db.ObjectStateManager.ChangeObjectState(notification, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer(Notification notif)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Notifications.DeleteObject(notif);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
     
   

    }
}
